<?php
namespace app\admin\controller;

use app\common\model\CompanyGongdengOrder as CompanyGongdengOrderModel;
use app\common\model\Company as CompanyModel;

/**
 * 寺院订单明细控制器
 * Class Index
 * @package app\index\controller
 */
class Companygongdengorder extends Base
{
    /**
     * [index  明细列表]
     * @param type 1供灯 2行善 3祈福香 4日行一善 5祈福蜡烛 6供十方 7许愿
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new CompanyGongdengOrderModel;
        $cmodel = new CompanyModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try {
            if (!empty($param['username'])) {
                $map['m.username'] = ['like', "%" . $param['username'] . "%"];
            }
            if (!empty($param['tel'])) {
                $map['m.tel'] = ['like', "%" . $param['tel'] . "%"];
            }
            if (!empty($param['sex'])) {
                $map['m.sex'] = $param['sex'];
            }
            if (!empty($param['start']) || !empty($param['end'])) {
                $map['c.create_time'] = ['between', [strtotime($param['start']), strtotime($param['end'])]];
            }
            if (!empty($param['mid'])) {
                $map['c.mid'] = $param['mid'];
            }

            if (empty($param['type'])) {
                $field = 'c.id,c.comid,c.mid,c.gongdeng,c.day,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                $name = '总订单';
            } else {
                $map['c.type'] = $param['type'];

                if ($param['type'] == 1) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.day,c.name,c.to_name,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '供灯';
                }
                if ($param['type'] == 2) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '行善';
                }
                if ($param['type'] == 3) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '祈福香';
                }
                if ($param['type'] == 4) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.name,c.tel,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '日行一善';
                }
                if ($param['type'] == 5) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.day,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '祈福蜡烛';
                }
                if ($param['type'] == 6) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '供十方';
                }
                if ($param['type'] == 7) {
                    $field = 'c.id,c.comid,c.mid,c.gongdeng,c.day,c.name,c.to_name,c.content,c.order_num,c.total_price,c.paytime,c.status,c.create_time,m.img,m.username,m.tel mtel,m.sex';
                    $name = '许愿';
                }
            }
            if($user['is_zhu'] == 2){
                $map['admin_id'] = $user['id'];
            }
            $count = $model->getCount($map,$user['is_zhu']);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map, $this->from, $this->size, $field,$user['is_zhu']);
            foreach ($lists as &$v) {
                $company = $cmodel->findData($v['comid'],'id','comname');
                $http = substr($v['img'], 0, 4);
                if ($http != 'http') {
                    $v['img'] = config('domain_name') . $v['img'];
                }
                if (!empty($v['paytime'])) {
                    $v['paytime'] = date('Y-m-d H:i:s', $v['paytime']);
                }
                $v['comname'] = $company['comname'];
            }
        } catch (\Exception $e) {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }
        writelog('查看'.$name.'列表', 0,$user);
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
}